﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace M3G {
    public abstract class Shader : Object3D {
        #region Field
        byte[] source;
        string log;
        #endregion

        #region Method
        protected Shader (string fileName) {
            if (fileName == null) {
                throw new ArgumentNullException ("Source is null");
            }
            if (fileName == "") {
                throw new ArgumentException ("Source is empty");
            }

            using (FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read)) {
                this.source = new byte[fs.Length];
                fs.Read (this.source, 0, (int)fs.Length);
            }
            this.log = "Not compiled";
        }

        protected Shader (MemoryStream memoryStream) {
            if (memoryStream == null) {
                throw new ArgumentNullException ("Source is null");
            }

            this.source = new byte[memoryStream.Length];
            memoryStream.Read (this.source, 0, (int)memoryStream.Length);
            this.log = "Not compiled";
        }

        protected Shader (Shader shader) : base(shader) {
            this.source = (byte[])shader.source.Clone();
            this.log = (string)shader.log.Clone ();
        }


        public string InfoLog {
            get { return log; }
        }

        public string Source {
            get { 
                return new StreamReader(new MemoryStream(source)).ReadToEnd();
            }
        }

        internal void SetInfoLog (string log) {
            this.log = log;
        }
        #endregion
    }
}
